package cn.iocoder.yudao.module.tms.service.orderfollowlog;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;

import java.util.List;

import cn.iocoder.yudao.module.tms.dal.dataobject.orderfollowlog.OrderFollowLogAttachmentDO;
import cn.iocoder.yudao.module.tms.dal.mysql.orderfollowlog.OrderFollowLogAttachmentMapper;

/**
 * 产品跟进记录附件 Service 实现类
 *
 * @author 芋道源码
 */
@Service
public class OrderFollowLogAttachmentServiceImpl implements OrderFollowLogAttachmentService {

    @Resource
    private OrderFollowLogAttachmentMapper orderFollowLogAttachmentMapper;

    @Override
    public List<OrderFollowLogAttachmentDO> getAttachmentsByFollowLogId(Long followLogId) {
        return orderFollowLogAttachmentMapper.selectListByFollowLogId(followLogId);
    }

    @Override
    public void saveFollowLogAttachments(Long followLogId, List<OrderFollowLogAttachmentDO> attachments) {
        // 先删除原有的附件记录
        orderFollowLogAttachmentMapper.deleteByFollowLogId(followLogId);

        // 如果有新的附件列表，则插入新记录
        if (CollUtil.isNotEmpty(attachments)) {
            for (OrderFollowLogAttachmentDO attachment : attachments) {
                attachment.setFollowLogId(followLogId);
                orderFollowLogAttachmentMapper.insert(attachment);
            }
        }
    }

    @Override
    public void deleteAttachmentsByFollowLogId(Long followLogId) {
        orderFollowLogAttachmentMapper.deleteByFollowLogId(followLogId);
    }

}